package com.brutal.paint;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.event.MouseEvent;
import java.util.ArrayList;

public class Paint {

	private ArrayList<PaintTab> tabs = new ArrayList<PaintTab>();
	private PaintProgressBar progressBar = null;
	private ArrayList<Boolean> openTabs = new ArrayList<Boolean>();

	public Paint(int skill, Image tab1, Image tab2, Image tab3,
			String tab1Text, String tab2Text, String tab3Text) {
		progressBar = new PaintProgressBar(skill, 8, 317, 424, 16);
		tabs.add(new PaintTab(tab1, tab1Text, 437, 312));
		tabs.add(new PaintTab(tab2, tab2Text, 463, 312));
		tabs.add(new PaintTab(tab3, tab3Text, 489, 312));
		openTabs.add(false);
		openTabs.add(false);
		openTabs.add(false);
	}

	private boolean closeAllTabs() {
		for (int i = 0; i < openTabs.size(); i++) {
			openTabs.set(i, false);
		}
		return tabs.get(0).close() && tabs.get(1).close()
				&& tabs.get(2).close();
	}

	public PaintTab getTab(int tabNumber) {
		return tabs.get(tabNumber - 1);
	}

	public void updateTabText(PaintTab tab, String text) {
		tab.updateText(text);
	}

	private boolean otherTabsHovered(int index) {
		for (int i = 0; i < tabs.size(); i++) {
			if (i == index) {
				continue;
			}
			if (tabs.get(i).isHovered()) {
				return true;
			}
		}
		return false;
	}

	public void onRepaint(Graphics g1) {
		Graphics2D g = (Graphics2D) g1;
		g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
		g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		if (tabs.get(0) != null) {
			tabs.get(0).onRepaint(g);
		}
		if (tabs.get(1) != null) {
			tabs.get(1).onRepaint(g);
		}
		if (tabs.get(2) != null) {
			tabs.get(2).onRepaint(g);
		}
		if (progressBar != null) {
			progressBar.onRepaint(g);
		}
	}

	public void mouseMoved(MouseEvent e) {
		for (int i = 0; i < tabs.size(); i++) {
			if (tabs.get(i) == null) {
				continue;
			}
			tabs.get(i).close();
			if (tabs.get(i).getBounds() == null) {
				continue;
			}
			if (!tabs.get(i).getBounds().contains(e.getPoint())) {
				tabs.get(i).close();
				continue;
			}
			tabs.get(i).hover();
		}
		if (tabs.get(0) == null || tabs.get(1) == null || tabs.get(2) == null) {
			return;
		}
		for (int i = 0; i < openTabs.size(); i++) {
			if (openTabs.get(i) == null || !openTabs.get(i)
					|| otherTabsHovered(i)) {
				continue;
			}
			tabs.get(i).open();
		}
	}

	public void mouseClicked(MouseEvent e) {
		if (e.getButton() != MouseEvent.BUTTON1) {
			return;
		}
		for (int i = 0; i < tabs.size(); i++) {
			if (tabs.get(i) == null) {
				continue;
			}
			if (tabs.get(i).getBounds() == null) {
				continue;
			}
			if (!tabs.get(i).getBounds().contains(e.getPoint())) {
				continue;
			}
			if (tabs.get(i).isOpen()) {
				tabs.get(i).close();
				openTabs.set(i, false);
			} else if (!tabs.get(i).isOpen()) {
				closeAllTabs();
				tabs.get(i).open();
				openTabs.set(i, true);
			}
		}
	}
}
